package cn.com.wdjedu.androidapp.wjeduandroid.support.lib.encrypt;

import android.support.annotation.NonNull;
import android.util.Base64;

import java.io.UnsupportedEncodingException;

import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.pulltorefresh.Utility;

/**
 * 解码个人头像 2017/1/13.
 */

public class EncryptPersonHead implements EncryptBase {
    private final int ENCRYPT_FLAG = 8888;

    @Override
    public String encrypt(@NonNull String value) {
        return encrypt(value.getBytes());
    }

    @Override
    public String encrypt(int value) {
        return encrypt(String.valueOf(value));
    }

    @Override
    public String encrypt(long value) {
        return encrypt(String.valueOf(value));
    }

    @Override
    public String encrypt(byte[] value) {
        return Base64.encodeToString(value, 0, value.length, ENCRYPT_FLAG);
    }

    // data:image/jpeg;base64,%252f9j%252f4AAQ...
    @Override
    public String decrypt(@NonNull String value) {
        String fileName = "PersonHeadImage";
        fileName = Utility.md5(fileName);
        fileName += ".png";

        byte[] bytes = new byte[0];
        try {
            bytes = Base64.decode(value.getBytes("utf-8"), ENCRYPT_FLAG);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return fileName + "-" + new String(bytes);
    }
}
